package com.china08.yxyapi.api.manage.sch.platform.xiaoben.catalog;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolCatalog;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolCatalogRepository;

/**
 * 校本课程--编辑子分类
 * 
 * @author LiuWeidong
 *
 */

@CrossOrigin
@RestController
public class XiaobenCatalogTwoCrtController extends YxyApiBaseController {

    @Autowired
    private YxyLessonsSchoolCatalogRepository yxyLessonsSchoolCatalogRepository;

    @RequestMapping(value = "/api_yw/manage/sch/platform/xiaoben/catalog/two/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel model) {

        String idOne = model.idOne;
        String idTwo = model.idTwo;
        String nameTwo = model.nameTwo;
        String schoolIdTwo = model.schoolIdTwo;

        YxyLessonsSchoolCatalog yxyLessonsSchoolCatalogs = yxyLessonsSchoolCatalogRepository
                .findByNameAndLevelAndSchoolIdAndPid(nameTwo, 2, schoolIdTwo, idOne);
        Assert.isTrue(yxyLessonsSchoolCatalogs == null, "请不要修改成重复的分类名称！");

        YxyLessonsSchoolCatalog yxyLessonsSchoolCatalog = yxyLessonsSchoolCatalogRepository
                .findByIdAndPidAndSchoolId(idTwo, idOne, schoolIdTwo);
        if (yxyLessonsSchoolCatalog != null) {
            yxyLessonsSchoolCatalog.setName(nameTwo);
            yxyLessonsSchoolCatalogRepository.save(yxyLessonsSchoolCatalog);
        }
    }

    /** 请求参数 */
    protected static class ReqModel extends YxyApiReqModel {

        // 父分类ID
        public String idOne;
        // 子分类ID
        public String idTwo;
        // 子分类名称
        public String nameTwo;
        // 子分类学校ID
        public String schoolIdTwo;

    }
}
